function getMetaId() {
    let date = Date.now().toString(36);
    let number = Math.random() * 100000000000;
    let random = parseInt(number).toString(32)
    return date + random;
}
function switchToPaths(paths, path) {
    let oriPath = paths.filter(item => item.type == path.type).find(item => true);
    if(oriPath != null) {
        return Object.assign(oriPath, path);
    } else {
        return paths.push(path);
    }
}

function addToTabMetas(tabMetas, key, metas) {
    let tabMeta = tabMetas[key];
    if(!tabMeta) {
        throw new Error(`tabMetas对象不存在字段(${key}), ${tabMetas}`)
    }

    tabMeta['metas'].push(metas);
}
function addToTabMetasBefore(tabMetas, key, metas) {
    let tabMeta = tabMetas[key];
    if(!tabMeta) {
        throw new Error(`tabMetas对象不存在字段(${key}), ${tabMetas}`)
    }

    tabMeta['metas'].splice(0, 0, ...metas);
}
function addToTabMetasBeforeAt(tabMetas, key, field, metas) {
    let tabMeta = tabMetas[key];
    if(!tabMeta) {
        throw new Error(`tabMetas对象不存在字段(${key}), ${tabMetas}`)
    }

    for(let index in tabMeta['metas']) {
        let meta = tabMeta['metas'][index];
        if(meta.field == field) {
            tabMeta['metas'].splice(index, 0, ...metas);
            return;
        }
    }
}

function addToTabMetasAfterAt(tabMetas, key, field, ...metas) {
    let tabMeta = tabMetas[key];
    if(!tabMeta) {
        throw new Error(`tabMetas对象不存在字段(${key}), ${tabMetas}`)
    }

    for(let index in tabMeta['metas']) {
        let meta = tabMeta['metas'][index];
        if(meta.field == field) {
            tabMeta['metas'].splice(index + 1, 0, ...metas);
            return;
        }
    }
}

function delFromTabMetas(tabMetas, key, fields) {
    let tabMeta = tabMetas[key];
    if(!tabMeta) {
        throw new Error(`tabMetas对象不存在字段(${key}), ${tabMetas}`)
    }
    fields.forEach(field => {
        for(let index in tabMeta['metas']) {
            let meta = tabMeta['metas'][index];
            if(meta.field == field) {
                tabMeta['metas'].splice(index, 1);
                break;
            }
        }
    })
}

function cloneMeta(meta) {
    let stringify = JSON.stringify(meta);
    return JSON.parse(stringify);
}

function cloneTabMetas(tabMetas) {
    let clone = {... tabMetas};
    Object.keys(clone).forEach(key => {
        let value = tabMetas[key];
        clone[key] = {label: value.label, metas: [...value.metas]};
    });

    return clone;
}

function includeMetas(metas, id) {
    for(let index in metas) {
        let meta = metas[index];
        if(meta.id == id) {
            return index;
        }
    }
    return -1;
}

function getMetaById(metas, id) {
    let index = includeMetas(metas, id);
    return index > -1 ? metas[index] : null;
}

function getTabMetaByTabMetas(tabMetas, field) {
    for(let key in tabMetas) {
        let metas = tabMetas[key]['metas'];
        return metas.find(item => item.field == field ? item : null);
    }
}

export default {switchToPaths, addToTabMetas, delFromTabMetas, includeMetas, getMetaById
    , addToTabMetasAfterAt, addToTabMetasBeforeAt, addToTabMetasBefore, cloneTabMetas
    , getTabMetaByTabMetas, getMetaId, cloneMeta}
